
package com.market.web.action;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.market.bean.Customer;
import com.market.bean.Subknowledge;
import com.market.bean.Subnews;
import com.market.web.form.CustomerForm;

public class CustomerAction extends BaseAction2
{
    protected Customer setBaseInf(CustomerForm cusf, Customer cus)
    {
        cus.setAddress(cusf.getAddress());
        cus.setName(cusf.getName());
        cus.setPass(cusf.getPass());
        cus.setSex(cusf.isSex());
        cus.setTel(cusf.getTel());
        cus.setWork(cusf.getWork());
        cus.setZip(cusf.getZip());
        cus.setRegdate(new Date());
        cus.setLastlogin(new Date());
        cus.setLevel(0);
        if (cusf.getBirthday() != null)
        {
            DateFormat formater = DateFormat.getDateInstance();
            try
            {
                cus.setBirth(formater.parse(cusf.getBirthday()));
            }
            catch (Exception e)
            {
                logger.error("",e);
            }
        }
        return cus;
    }

    protected void setFavourInfo(CustomerForm cusf, Customer cus)
    {
        String[] knowledges = cusf.getSubknow();
        if (knowledges != null)
        {
            Set sk = new HashSet();
            for (int i = 0; i < knowledges.length; i++)
            {
                Subknowledge know = new Subknowledge();
                know.setCategory(knowledges[i]);
                know.setCusid(cus.getId());
                sk.add(know);
            }
            cus.setSubknowledges(sk);
        }
        String[] news = cusf.getSubnews();
        if (news != null)
        {
            Set sn = new HashSet();
            for (int i = 0; i < news.length; i++)
            {
                Subnews subnews = new Subnews();
                subnews.setCategory(news[i]);
                subnews.setCusid(cus.getId());
                sn.add(subnews);
            }
            cus.setSubnewses(sn);
        }
    }
}
